package com.cuihanyu.newuniversityover.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.util.CodeUtils;
import com.cuihanyu.newuniversityover.util.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cuihanyu on 2018/4/3.
 */

public class RegisterActivity extends AppCompatActivity {
    @BindView(R.id.Code_ImageView)
    ImageView mCode_ImageView;
    @BindView(R.id.ZC_Button)
    Button mZC_Button;
    @BindView(R.id.name)
    EditText mName;
    @BindView(R.id.Phone_number)
    EditText mPhone_number;
    @BindView(R.id.Code)
    EditText mCode;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        intent = getIntent();
        initView();
    }

    private void initView() {
        mCode_ImageView.setImageBitmap(CodeUtils.getInstance().createBitmap());
    }

    @OnClick({R.id.ZC_Button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ZC_Button:
                if (!mName.getText().toString().isEmpty() && !mPhone_number.getText().toString().isEmpty()&&
                        mPhone_number.getText().toString().length()==11
                        && !mCode.getText().toString().isEmpty() &&
                        CodeUtils.getInstance().getCode().equals(mCode.getText().toString())) {
                    SPUtils.put(this,"Name",mName.getText().toString());
                    SPUtils.put(this,"Phone",mPhone_number.getText().toString());
                    intent.putExtra("Name",mName.getText().toString());
                    intent.putExtra("Phone",mPhone_number.getText().toString());
                    setResult(1,intent);
                    finish();
                } else {
                    Toast.makeText(this, "输入的信息不正确~", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
